package com.heima.article.listener;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.heima.article.service.ApArticleConfigService;
import com.heima.common.constants.WmNewsMessageConstants;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Component;

import java.util.Map;

/**
 * @author itlxc
 * @since 2024-07-28
 */
@Component
@Slf4j
public class ArtilceIsDownListener {


    @Autowired
    private ApArticleConfigService apArticleConfigService;

    /**
     * 文章上下架消息处理
     * @param message
     */
    @KafkaListener(topics = WmNewsMessageConstants.WM_NEWS_UP_OR_DOWN_TOPIC)
    public void onMessage(String message) {
        log.info("收到消息：{}", message);
        if (StrUtil.isNotBlank(message)) {
            // 1.解析消息
            Map<String, Object> map = JSONUtil.toBean(message, Map.class);
            // 2.修改文章配置表
            apArticleConfigService.updateByMap(map);
        }
    }
}
